<script setup>
import { onMounted, ref, reactive, nextTick } from "vue";
import { Plus } from "@element-plus/icons-vue";
import { menuDataApi, menuListApi, permissionApi } from "../../../api/user";

const page = reactive({
  pageNum: 1,
  pageSize: 10,
});
const handleSizeChange = (val) => {
  page.pageSize = val;
  permissionList();
};
const handleCurrentChange = (val) => {
  page.pageNum = val;
  permissionList();
};
// 表单数据
const form = ref({
  name: "", //权限名称
  permissions: "", // 选中权限
});
// 树形数据
const treeData = ref([]);
// 默认选项
const defaultCheckedKeys = ref([4, 5]);
// 获取权限列表
const permissionData = async () => {
  const res = await menuDataApi(page);
  treeData.value = res.data;
};

// 弹窗控制
const dialogVisible = ref(false);
//  关闭弹窗
const handleClose = () => {
  // 重置表单字段
  form.value = {
    name: "",
    permissions: "",
  };
  // 清除树形控件的选中状态
  if (treeRef.value) {
    treeRef.value.setCheckedKeys(defaultCheckedKeys.value);
  }
  dialogVisible.value = false;
};
const rules = {
  name: [{ required: true, message: "请输入权限名", trigger: "blur" }],
};

const formRef = ref(null);
const treeRef = ref(null);
// 提交表单
const confirm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      // 获取选中的节点
      // 将树形控件中选中的节点的 key 值转换为字符串，存储到表单的 permissions 字段中
      // 这里的 treeRef 是对树形控件的引用，getCheckedKeys 方法获取当前选中的节点的 key 值数组
      // 使用 JSON.stringify 方法将数组转换为 JSON 字符串，以便存储和传输
      form.value.permissions = JSON.stringify(treeRef.value.getCheckedKeys());
      const res = await permissionApi(form.value);
      if (res.code === 10000) {
        ElMessage.success("添加成功");
        handleClose();
        // 刷新表格数据
        permissionList();
      }
    } else {
      console.log("校验失败");
      return false;
    }
  });
};
// 表格数据
const tableData = ref([]);
const total = ref(0);
// 获取权限列表
const permissionList = async () => {
  const res = await menuListApi(page);
  tableData.value = res.data.list;
  total.value = res.data.total;
};

onMounted(() => {
  permissionData();
  permissionList();
});
const title = ref("添加权限");
// 编辑/添加
const handleOpen = (row) => {
  // 添加
  if (!row) {
    title.value = "添加权限";
    dialogVisible.value = true;
  } else if (row) {
    title.value = "编辑权限";
    dialogVisible.value = true;
    // 点击编辑按钮,将数据回填
    nextTick(() => {
      if (row) {
        form.value = { ...row };
        // 将 permissions 字符串解析为数组
        treeRef.value.setCheckedKeys(row.permission);
      }
    });
  }
};
// 删除
const handleDel = (row) => {};
</script>

<template>
  <panelHead />
  <el-card>
    <el-button type="primary" size="small" :icon="Plus" @click="handleOpen()">
      新增
    </el-button>
    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%">
      <!-- 序号 -->
      <el-table-column label="序号" width="180">
        <template #default="scope">
          {{ (page.pageNum - 1) * page.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="id" label="ID" width="180" />
      <el-table-column prop="name" label="昵称" width="180" />
      <el-table-column prop="permissionName" label="菜单权限" />
      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <el-button type="primary" size="small" @click="handleOpen(row)">
            编辑
          </el-button>
          <el-button type="danger" size="small" @click="handleDel(roe)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-info">
      <el-pagination
        :v-model:currentPage="page.pageNum"
        :page-size="page.pageSize"
        layout="total, prev,
  pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </el-card>

  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="500"
    :before-close="handleClose"
  >
    <el-form
      ref="formRef"
      :rules="rules"
      :model="form"
      label-width="100px"
      label-position="left"
    >
      <el-form-item label="名称" prop="name" required>
        <el-input v-model="form.name" placeholder="请填写权限名称" />
      </el-form-item>

      <el-form-item label="权限" prop="permissions">
        <el-tree
          ref="treeRef"
          style="max-width: 600px"
          :data="treeData"
          show-checkbox
          node-key="id"
          :default-expanded-keys="[2]"
          :default-checked-keys="defaultCheckedKeys"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" size="small" @click="confirm(formRef)">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped></style>
